//
//  OverlayAnimationController.m
//  CustomModalTransition
//
//  Created by 朱志佳 on 2019/4/4.
//  Copyright © 2019 朱志佳. All rights reserved.
//

#define kSystemVersionF [[[UIDevice currentDevice] systemVersion] floatValue]

#import "OverlayAnimationController.h"

@implementation OverlayAnimationController

// This is used for percent driven interactive transitions, as well as for
// container controllers that have companion animations that might need to
// synchronize with the main animation.
- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext{
    NSLog(@"%@##%@",NSStringFromClass([self class]),NSStringFromSelector(_cmd));
    return 0.3;
}
// This method can only  be a nop if the transition is interactive and not a percentDriven interactive transition.
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext {
    NSLog(@"%@##%@",NSStringFromClass([self class]),NSStringFromSelector(_cmd));
    UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];

    UIView *containerView = [transitionContext containerView];

    UIView *fromView = fromVC.view;
    UIView *toView = toVC.view;
    CGFloat duration = [self transitionDuration:transitionContext];

    if (toVC.isBeingPresented) {
        [containerView addSubview:toView];
        CGFloat toViewWidth = containerView.frame.size.width * 2 / 3;
        CGFloat toViewHeight = containerView.frame.size.height * 2 / 3;
        toView.center = containerView.center;
        toView.bounds = CGRectMake(0, 0, 1, toViewHeight);

        [UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionTransitionNone animations:^{
            toView.bounds = CGRectMake(0, 0, toViewWidth, toViewHeight);
        } completion:^(BOOL finished) {
            BOOL isCancelled = [transitionContext transitionWasCancelled];
            [transitionContext completeTransition:!isCancelled];
        }];
    }

    //Dismissal 转场中不要将 toView 添加到 containerView
    if (fromVC.isBeingDismissed) {
        CGFloat fromViewHeight = fromView ? fromView.frame.size.height:0 ;
        [UIView animateWithDuration:duration animations:^{
            fromView.bounds = CGRectMake(0, 0, 1, fromViewHeight);
        } completion:^(BOOL finished) {
            BOOL isCancelled = [transitionContext transitionWasCancelled];
            [transitionContext completeTransition:!isCancelled];
        }];
    }
}

/// A conforming object implements this method if the transition it creates can
/// be interrupted. For example, it could return an instance of a
/// UIViewPropertyAnimator. It is expected that this method will return the same
/// instance for the life of a transition.
//- (id <UIViewImplicitlyAnimating>) interruptibleAnimatorForTransition:(id <UIViewControllerContextTransitioning>)transitionContext NS_AVAILABLE_IOS(10_0);

// This is a convenience and if implemented will be invoked by the system when the transition context's completeTransition: method is invoked.
//- (void)animationEnded:(BOOL) transitionCompleted{
//    NSLog(@"%@##%@",NSStringFromClass([self class]),NSStringFromSelector(_cmd));
//}

@end
